package com.lt.bcra.controller.dictionary;


import com.lt.bcra.entity.BcraCategory;
import com.lt.bcra.entity.BcraDictionary;
import com.lt.bcra.exception.BaseException;
import com.lt.bcra.result.ResponseEnum;
import com.lt.bcra.result.ResultData;
import com.lt.bcra.service.BcraCategoryService;
import com.lt.bcra.service.BcraDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LuoTian
 * @since 2022-05-30
 */
@Api(tags = "字典数据表")
@CrossOrigin //跨域可以前端配置
@RestController
@RequestMapping("/bcra/dictionary")
public class BcraDictionaryController {

    private static Logger log = LoggerFactory.getLogger(BcraDictionaryController.class);

    @Autowired
    BcraDictionaryService bcraDictionaryService;

    @GetMapping("/getDictionary/{categoryId}/{current}/{size}")
    @ApiOperation("根据种类id查看字典")
    public ResultData list(@PathVariable Integer categoryId,
                           @PathVariable Long current,
                           @PathVariable Long size) throws BaseException {
        if (log.isDebugEnabled()) log.debug("查看字典表 BcraDictionaryController");

        return bcraDictionaryService.listByCategoryId(categoryId,current,size);
    }

    @GetMapping("/getById/{id}")
    @ApiOperation("根据id查看字典")
    public ResultData listById(@PathVariable Integer id) throws BaseException {
        if (log.isDebugEnabled()) log.debug("根据id查看字典 BcraDictionaryController");

        BcraDictionary bcraDictionary = bcraDictionaryService.getById(id);

        if (bcraDictionary==null){
            throw new BaseException(ResponseEnum.SELECT_DATA_ERROR);
        }
        return ResultData.getResult(bcraDictionary);
    }

    @DeleteMapping("/deleteDictionary/{id}")
    @ApiOperation("删除字典数据表")
    @ApiParam(value = "id")
    public ResultData delete(@PathVariable Integer id) throws BaseException{
        if (log.isDebugEnabled()) log.debug("删除字典数据 BcraDictionaryController");

        boolean remove = bcraDictionaryService.removeById(id);
        if (!remove){
            throw new BaseException(ResponseEnum.DELETE_DATA_ERROR);
        }
        return ResultData.getResult(remove);
    }

    @PostMapping("/creatDictionary")
    @ApiOperation("创建字典数据表")
    @ApiParam(value = "bcraDictionary")
    public ResultData creatNew(@RequestBody BcraDictionary bcraDictionary) throws BaseException {
        if (log.isDebugEnabled()) log.debug("创建字典种类表 BcraDictionaryController");

        return bcraDictionaryService.saveDictionary(bcraDictionary);
    }

    @PostMapping("/updateDictionary")
    @ApiOperation("修改字典数据表")
    @ApiParam(value = "bcraDictionary")
    public ResultData update(@RequestBody BcraDictionary bcraDictionary) throws BaseException {
        if (log.isDebugEnabled()) log.debug("修改字典数据表 BcraDictionaryController");

        boolean update = bcraDictionaryService.updateById(bcraDictionary);
        if (!update){
            throw new BaseException(ResponseEnum.UPDATE_DATA_ERROR);
        }
        return ResultData.getResult(update);
    }

}

